package service;

import dao.UserDao;
import model.User;
import util.SessionManager;
import java.util.List;
import java.util.Scanner;

public class UserService {
    private static UserDao userDao;
    private static Scanner scanner;

    public UserService() {
        this.userDao = new UserDao();
        this.scanner = new Scanner(System.in);
    }

    public static boolean login() {
        System.out.println("\n===== 用户登录 =====");
        System.out.print("请输入用户名: ");
        String username = scanner.nextLine();
        System.out.print("请输入密码: ");
        String password = scanner.nextLine();

        if (userDao.validatePassword(username, password)) {
            User user = userDao.findByUsername(username);
            SessionManager.setCurrentUser(user);
            System.out.println("登录成功！欢迎 " + user.getRealName() + " [" + user.getRole() + "]");
            return true;
        } else {
            System.out.println("登录失败！用户名或密码错误");
            return false;
        }
    }

    public void logout() {
        SessionManager.clear();
        System.out.println("已安全退出系统");
    }

    public void changePassword() {
        if (!SessionManager.isLoggedIn()) {
            System.out.println("请先登录");
            return;
        }

        System.out.println("\n===== 修改密码 =====");
        System.out.print("请输入当前密码: ");
        String currentPassword = scanner.nextLine();
        System.out.print("请输入新密码: ");
        String newPassword = scanner.nextLine();
        System.out.print("请确认新密码: ");
        String confirmPassword = scanner.nextLine();

        String username = SessionManager.getCurrentUser().getUsername();

        if (!userDao.validatePassword(username, currentPassword)) {
            System.out.println("当前密码错误");
            return;
        }

        if (!newPassword.equals(confirmPassword)) {
            System.out.println("两次输入的新密码不一致");
            return;
        }

        if (newPassword.length() < 6) {
            System.out.println("密码长度不能少于6位");
            return;
        }

        userDao.changePassword(username, newPassword);
        System.out.println("密码修改成功");
    }

    public void addUser() {
        if (!SessionManager.isLoggedIn() || !SessionManager.isAdmin()) {
            System.out.println("权限不足，需要管理员权限");
            return;
        }

        System.out.println("\n===== 添加用户 =====");
        System.out.print("请输入用户名: ");
        String username = scanner.nextLine();

        if (userDao.findByUsername(username) != null) {
            System.out.println("用户名已存在");
            return;
        }

        System.out.print("请输入密码: ");
        String password = scanner.nextLine();
        System.out.print("请输入真实姓名: ");
        String realName = scanner.nextLine();
        System.out.print("请输入角色 (admin/manager/staff): ");
        String role = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();
        System.out.print("请输入邮箱: ");
        String email = scanner.nextLine();

        User user = new User(username, password, role, realName);
        user.setPhone(phone);
        user.setEmail(email);

        userDao.addUser(user);
        System.out.println("用户添加成功");
    }

    public void viewAllUsers() {
        if (!SessionManager.isLoggedIn() || !SessionManager.isAdmin()) {
            System.out.println("权限不足，需要管理员权限");
            return;
        }

        System.out.println("\n===== 所有用户 =====");
        List<User> users = userDao.getAllUsers();

        if (users.isEmpty()) {
            System.out.println("暂无用户数据");
            return;
        }

        System.out.println("用户名\t真实姓名\t角色\t电话\t\t邮箱\t\t状态");
        System.out.println("----------------------------------------------------------------");
        for (User user : users) {
            System.out.printf("%s\t%s\t%s\t%s\t%s\t%s\n",
                    user.getUsername(), user.getRealName(), user.getRole(),
                    user.getPhone(), user.getEmail(), user.isActive() ? "激活" : "禁用");
        }
    }

    public void showCurrentUser() {
        if (!SessionManager.isLoggedIn()) {
            System.out.println("未登录");
            return;
        }

        User user = SessionManager.getCurrentUser();
        System.out.println("\n===== 当前用户信息 =====");
        System.out.println("用户名: " + user.getUsername());
        System.out.println("真实姓名: " + user.getRealName());
        System.out.println("角色: " + user.getRole());
        System.out.println("电话: " + user.getPhone());
        System.out.println("邮箱: " + user.getEmail());
    }
}